{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6ef7acef",
   "metadata": {},
   "source": [
    "# Weather Agent\n",
    "\n",
    "### Equipped with a few MCP tools using the Weather API, to get the actual weather of any city around the world\n",
    "\n",
    "You need to get your free API keys from the [Weather API](https://www.weatherapi.com/) to try it out\n",
    "\n",
    "Save the generated key as WEATHERAPI_API_KEY to your .env file\n",
    "\n",
    "Enjoy :)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "28ab27eb",
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.display import display, Markdown\n",
    "from agents import Agent, Runner, trace\n",
    "from agents.mcp import MCPServerStdio\n",
    "from dotenv import load_dotenv\n",
    "from weather import Weather \n",
    "\n",
    "load_dotenv(override=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dfee5591",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Test the weather API\n",
    "print(Weather.get_temperature_by_city(\"New York\"))\n",
    "print(Weather.get_weather_by_city(\"New York\"))\n",
    "print(Weather.get_humidity_by_city(\"New York\"))\n",
    "print(Weather.get_wind_by_city(\"New York\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5295d009",
   "metadata": {},
   "outputs": [],
   "source": [
    "params = {\"command\": \"uv\", \"args\": [\"run\", \"weather_server.py\"]}\n",
    "async with MCPServerStdio(params=params, client_session_timeout_seconds=30) as server:\n",
    "    mcp_tools = await server.list_tools()\n",
    "\n",
    "mcp_tools"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0ea3a827",
   "metadata": {},
   "outputs": [],
   "source": [
    "instructions = \"You are able answer quastions about the actual weather of a given city. When a user asks about the weather, try to give a detailed answer.\"\n",
    "request = \"What's the weather like in New York?\"\n",
    "model = \"gpt-4.1-mini\"  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "88cd5638",
   "metadata": {},
   "outputs": [],
   "source": [
    "async with MCPServerStdio(params=params, client_session_timeout_seconds=30) as mcp_server:\n",
    "    agent = Agent(name=\"weather_specialist\", instructions=instructions, model=model, mcp_servers=[mcp_server])\n",
    "    with trace(\"weather_specialist\"):\n",
    "        result = await Runner.run(agent, request)\n",
    "    display(Markdown(result.final_output))\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
